// Copyright 2007 Google Inc. All Rights Reserved.

package com.google.jessewilson.codesearch.ui;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;

/**
* @author jessewilson
*/
public class IntegerEditor implements EditorComponent<Integer> {
  private final JTextField component;

  public IntegerEditor() {
    component = new JTextField(5);
    component.setHorizontalAlignment(JTextField.RIGHT);
  }

  public JTextField getComponent() {
    return component;
  }

  public void setValue(Integer value) {
    component.setText(value == null ? "" : value.toString());
  }

  public Integer getValue() {
    try {
      return Integer.valueOf(component.getText());
    } catch (NumberFormatException e) {
      Logger.getLogger(IntegerEditor.class.getName())
          .log(Level.WARNING, "failed to parse line value match, \"" + component.getText() + "\"", e);
      return null;
    }
  }
}
